package Business;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.building_msmart.ulti.myUlti;

import DataAccess.SqlLiteDatabaseHandler;
import Entities.EAccount_;
import Entities.ECard_;
import Entities.ECome_in;
import Entities.ECustomer_;
import Entities.EGate_;
import Entities.EIdentityCard_deck;
import Entities.EIdentityCard_put;
import Entities.EIdentity_log;
import Entities.EOut_side;
import Entities.EOut_side_total;
import Entities.EUsers_;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class bSQL_Heper extends SqlLiteDatabaseHandler {

	public bSQL_Heper(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	// -------------Adding new Account_-------------------
	public void addAccount_(EAccount_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		//values.put("AcountID", v.getAcoutID());
		values.put("AccountName", v.getAccountName());
		values.put("Cus_ID", v.getCus_ID());
		values.put("Avata", v.getAvata());
		values.put("Photo", v.getPhoto());
		values.put("datepost", v.getdatepost());
		values.put("note", v.getnote());
		values.put("flag", v.getflag());
		values.put("UserID", v.getUserID());
		// Inserting Row
		db.insert(TABLE_ACCOUNT_, null, values);
		db.close(); // Closing database connection
	}

	// -------------Getting single Account_-------------------
	EAccount_ getAccount_(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_ACCOUNT_, new String[] { "AcountID",
				"AccountName", "Cus_ID", "Avata", "Photo", "datepost", "note",
				"flag", "UserID" }, "AcountID" + "=?",
				new String[] { String.valueOf(id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		EAccount_ v = new EAccount_(Integer.parseInt(cursor.getString(0)),
				cursor.getString(1), Integer.parseInt(cursor.getString(2)),
				cursor.getString(3), cursor.getString(4), cursor.getString(5),
				cursor.getString(6), cursor.getString(7),
				Integer.parseInt(cursor.getString(8)));
		db.close();
		return v;
	}

	// -------------Getting All Account_-------------------
	public List<EAccount_> getAllAccount_() {
		List<EAccount_> Account_List = new ArrayList<EAccount_>();

		// Select All Query
		String selectQuery = "SELECT  * FROM Account_";
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				EAccount_ v = new EAccount_();
				v.setAcoutID(Integer.parseInt(cursor.getString(0)));
				v.setAccountName(cursor.getString(1));
				v.setCus_ID(Integer.parseInt(cursor.getString(2)));
				v.setAvata(cursor.getString(3));
				v.setPhoto(cursor.getString(4));
				v.setdatepost(cursor.getString(5));
				v.setnote(cursor.getString(6));
				v.setflag(cursor.getString(7));
				v.setUserID(Integer.parseInt(cursor.getString(8)));

				// Adding contact to list
				Account_List.add(v);
			} while (cursor.moveToNext());
		}
		// return Account_ list
		db.close();
		return Account_List;
	}

	// Updating single Account_

	public int updateAccount_(EAccount_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("AcountID", v.getAcoutID());
		values.put("AccountName", v.getAccountName());
		values.put("Cus_ID", v.getCus_ID());
		values.put("Avata", v.getAvata());
		values.put("Photo", v.getPhoto());
		values.put("datepost", v.getdatepost());
		values.put("note", v.getnote());
		values.put("flag", v.getflag());
		values.put("UserID", v.getUserID());

		// updating row
		return db.update("Account_", values, "AcountID = ?",
				new String[] { String.valueOf(v.getAcoutID()) });
	}

	// Deleting single Account_
	public void deleteAccount_(EAccount_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("Account_", "AcountID = ?",
				new String[] { String.valueOf(v.getAcoutID()) });
		db.close();
	}

	// -------------Adding new IdentityCard_put-------------------
	public void addIdentityCard_put(EIdentityCard_put v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		//values.put("ID", v.getID());
		values.put("deckID", v.getdeckID());
		values.put("index", v.getindex());
		values.put("AcountID", v.getAcoutID());
		// Inserting Row
		db.insert(TABLE_IDENTITYCARD_PUT, null, values);
		db.close(); // Closing database connection
	}

	// -------------Getting single IdentityCard_put-------------------
	EIdentityCard_put getIdentityCard_put(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_IDENTITYCARD_PUT, new String[] { "ID",
				"deckID", "index", "AcountID" }, "ID" + "=?",
				new String[] { String.valueOf(id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		EIdentityCard_put v = new EIdentityCard_put(Integer.parseInt(cursor
				.getString(0)), Integer.parseInt(cursor.getString(1)),
				Integer.parseInt(cursor.getString(2)), Integer.parseInt(cursor
						.getString(3)));
		db.close();
		return v;
	}

	// -------------Getting All IdentityCard_put-------------------
	public List<EIdentityCard_put> getAllIdentityCard_put() {
		List<EIdentityCard_put> IdentityCard_putList = new ArrayList<EIdentityCard_put>();

		// Select All Query
		String selectQuery = "SELECT  * FROM IdentityCard_put";
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				EIdentityCard_put v = new EIdentityCard_put();
				v.setID(Integer.parseInt(cursor.getString(0)));
				v.setdeckID(Integer.parseInt(cursor.getString(1)));
				v.setindex(Integer.parseInt(cursor.getString(2)));
				v.setAcoutID(Integer.parseInt(cursor.getString(3)));

				// Adding contact to list
				IdentityCard_putList.add(v);
			} while (cursor.moveToNext());
		}
		// return IdentityCard_put list
		db.close();
		return IdentityCard_putList;
	}

	// Updating single IdentityCard_put

	public int updateIdentityCard_put(EIdentityCard_put v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("ID", v.getID());
		values.put("deckID", v.getdeckID());
		values.put("index", v.getindex());
		values.put("AcountID", v.getAcoutID());

		// updating row
		return db.update("IdentityCard_put", values, "ID = ?",
				new String[] { String.valueOf(v.getID()) });
	}

	// Deleting single IdentityCard_put
	public void deleteIdentityCard_put(EIdentityCard_put v) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("IdentityCard_put", "ID = ?",
				new String[] { String.valueOf(v.getID()) });
		db.close();
	}

	// -------------Adding new Gate_-------------------
	public void addGate_(EGate_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		//values.put("ID", v.getID());
		values.put("Name", v.getName());
		values.put("IPCamera", v.getIPCamera());
		values.put("flag", v.getflag());
		// Inserting Row
		db.insert(TABLE_GATE_, null, values);
		db.close(); // Closing database connection
	}

	// -------------Getting single Gate_-------------------
	EGate_ getGate_(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_GATE_, new String[] { "ID", "Name",
				"IPCamera", "flag" }, "ID" + "=?",
				new String[] { String.valueOf(id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		EGate_ v = new EGate_(Integer.parseInt(cursor.getString(0)),
				cursor.getString(1), cursor.getString(2), cursor.getString(3));
		db.close();
		return v;
	}

	// -------------Getting All Gate_-------------------
	public List<EGate_> getAllGate_() {
		List<EGate_> Gate_List = new ArrayList<EGate_>();

		// Select All Query
		String selectQuery = "SELECT  * FROM Gate_";
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				EGate_ v = new EGate_();
				v.setID(Integer.parseInt(cursor.getString(0)));
				v.setName(cursor.getString(1));
				v.setIPCamera(cursor.getString(2));
				v.setflag(cursor.getString(3));

				// Adding contact to list
				Gate_List.add(v);
			} while (cursor.moveToNext());
		}
		// return Gate_ list
		db.close();
		return Gate_List;
	}

	// Updating single Gate_

	public int updateGate_(EGate_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("ID", v.getID());
		values.put("Name", v.getName());
		values.put("IPCamera", v.getIPCamera());
		values.put("flag", v.getflag());

		// updating row
		return db.update("Gate_", values, "ID = ?",
				new String[] { String.valueOf(v.getID()) });
	}

	// Deleting single Gate_
	public void deleteGate_(EGate_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("Gate_", "ID = ?", new String[] { String.valueOf(v.getID()) });
		db.close();
	}

	// -------------Adding new Users_-------------------
	public void addUsers_(EUsers_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		//values.put("ID", v.getID());
		values.put("UserName", v.getUserName());
		values.put("Password", v.getPassword());
		values.put("Fullname", v.getFullname());
		values.put("CMND", v.getCMND());
		values.put("Role", v.getRole());
		values.put("flag", v.getflag());
		// Inserting Row
		db.insert(TABLE_USERS_, null, values);
		db.close(); // Closing database connection
	}

	// -------------Getting single Users_-------------------
	EUsers_ getUsers_(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_USERS_, new String[] { "ID", "UserName",
				"Password", "Fullname", "CMND", "Role", "flag" }, "ID" + "=?",
				new String[] { String.valueOf(id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		EUsers_ v = new EUsers_(Integer.parseInt(cursor.getString(0)),
				cursor.getString(1), cursor.getString(2), cursor.getString(3),
				cursor.getString(4), cursor.getString(5), cursor.getString(6));
		db.close();
		return v;
	}

	// -------------Getting All Users_-------------------
	public List<EUsers_> getAllUsers_(String query) {
		List<EUsers_> Users_List = new ArrayList<EUsers_>();

		// Select All Query
		String selectQuery = query;
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				EUsers_ v = new EUsers_();
				v.setID(Integer.parseInt(cursor.getString(0)));
				v.setUserName(cursor.getString(1));
				v.setPassword(cursor.getString(2));
				v.setFullname(cursor.getString(3));
				v.setCMND(cursor.getString(4));
				v.setRole(cursor.getString(5));
				v.setflag(cursor.getString(6));

				// Adding contact to list
				Users_List.add(v);
			} while (cursor.moveToNext());
		}
		// return Users_ list
		db.close();
		return Users_List;
	}

	// Updating single Users_

	public int updateUsers_(EUsers_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("ID", v.getID());
		values.put("UserName", v.getUserName());
		values.put("Password", v.getPassword());
		values.put("Fullname", v.getFullname());
		values.put("CMND", v.getCMND());
		values.put("Role", v.getRole());
		values.put("flag", v.getflag());

		// updating row
		return db.update("Users_", values, "ID = ?",
				new String[] { String.valueOf(v.getID()) });
	}

	// Deleting single Users_
	public void deleteUsers_(EUsers_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("Users_", "ID = ?",
				new String[] { String.valueOf(v.getID()) });
		db.close();
	}

	// -------------Adding new Customer_-------------------
	public void addCustomer_(ECustomer_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		//values.put("ID", v.getID());
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		Date date = new Date();
		
		String day="";
		String month="";
		String nam="";
		String chuoi2="";

		int idext=0;
		if(v.getBirthday()!="")
			idext = v.getBirthday().indexOf("/");
		day= v.getBirthday().substring(0, idext);
		chuoi2 = v.getBirthday().substring(idext+1);
		idext = chuoi2.indexOf("/");
		month = chuoi2.substring(0, idext);
		
		nam = chuoi2.substring(idext+1);
		
		String newday= nam+"/"+month+"/"+day;
		Log.d("rr", newday);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
		//dateb=String.format("MM/dd/yyyy",v.getBirthday());
		values.put("Fullname", v.getFullname());
		values.put("Sex", v.getSex());
		values.put("Birthday", sdf.format(Date.parse(newday)));
		values.put("Born", v.getBorn());
		values.put("CMND", v.getCMND());
		values.put("Address", v.getAddress());
		values.put("datepost", dateFormat.format(date));
		// Inserting Row
		Log.d("rr", "ket qua1:"+sdf.format(Date.parse(newday)));
		//Log.d("rr", "ket qua3:"+String.format("MM/dd/yyyy",v.getBirthday()));
		Log.d("rr", "Inserting Row");
		db.insert(TABLE_CUSTOMER_, null, values);
		db.close(); // Closing database connection
	}

	// -------------Getting single Customer_-------------------
	ECustomer_ getCustomer_(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_CUSTOMER_, new String[] { "ID",
				"Fullname", "Sex", "Birthday", "Born", "CMND", "Address",
				"datepost" }, "ID" + "=?", new String[] { String.valueOf(id) },
				null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		ECustomer_ v = new ECustomer_(Integer.parseInt(cursor.getString(0)),
				cursor.getString(1), cursor.getString(2), cursor.getString(3),
				cursor.getString(4), cursor.getString(5), cursor.getString(6),
				cursor.getString(7));
		db.close();
		return v;
	}

	// -------------Getting All Customer_-------------------
	public List<ECustomer_> getAllCustomer_() {
		List<ECustomer_> Customer_List = new ArrayList<ECustomer_>();

		// Select All Query
		String selectQuery = "SELECT  * FROM Customer_";
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				ECustomer_ v = new ECustomer_();
				v.setID(Integer.parseInt(cursor.getString(0)));
				v.setFullname(cursor.getString(1));
				v.setSex(cursor.getString(2));
				v.setBirthday(cursor.getString(3));
				v.setBorn(cursor.getString(4));
				v.setCMND(cursor.getString(5));
				v.setAddress(cursor.getString(6));
				v.setdatepost(cursor.getString(7));

				// Adding contact to list
				Customer_List.add(v);
			} while (cursor.moveToNext());
		}
		// return Customer_ list
		db.close();
		return Customer_List;
	}

	// Updating single Customer_

	public int updateCustomer_(ECustomer_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("ID", v.getID());
		values.put("Fullname", v.getFullname());
		values.put("Sex", v.getSex());
		values.put("Birthday", v.getBirthday());
		values.put("Born", v.getBorn());
		values.put("CMND", v.getCMND());
		values.put("Address", v.getAddress());
		values.put("datepost", v.getdatepost());

		// updating row
		return db.update("Customer_", values, "ID = ?",
				new String[] { String.valueOf(v.getID()) });
	}

	// Deleting single Customer_
	public void deleteCustomer_(ECustomer_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("Customer_", "ID = ?",
				new String[] { String.valueOf(v.getID()) });
		db.close();
	}
	// -------------Adding new Card_-------------------
	public void addCard_(ECard_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		//values.put("CardID", v.getCardID());
		values.put("datepost", v.getdatepost());
		values.put("flag", v.getflag());
		values.put("note", v.getnote());
		values.put("AcountID", v.getAcoutID());
		values.put("CardKey", v.getvCardKey());
		values.put("CardName", v.getCardName());
		// Inserting Row
		db.insert(TABLE_CARD_, null, values);
		db.close(); // Closing database connection
	}

	// -------------Getting single Card_-------------------
	ECard_ getCard_(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_CARD_, new String[] { "CardID",
				"datepost", "flag", "note", "AcountID" }, "CardID" + "=?",
				new String[] { String.valueOf(id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		ECard_ v = new ECard_(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
				cursor.getString(2), cursor.getString(3),
				Integer.parseInt(cursor.getString(4)),cursor.getString(5),cursor.getString(6));
		db.close();
		return v;
	}

	// -------------Getting All Card_-------------------
	public List<ECard_> getAllCard_() {
		List<ECard_> Card_List = new ArrayList<ECard_>();

		// Select All Query
		String selectQuery = "SELECT  * FROM Card_";
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				ECard_ v = new ECard_();
				v.setCardID(Integer.parseInt(cursor.getString(0)));
				v.setdatepost(cursor.getString(1));
				v.setflag(cursor.getString(2));
				v.setnote(cursor.getString(3));
				v.setAcoutID(Integer.parseInt(cursor.getString(4)));

				// Adding contact to list
				Card_List.add(v);
			} while (cursor.moveToNext());
		}
		// return Card_ list
		db.close();
		return Card_List;
	}

	// Updating single Card_

	public int updateCard_(ECard_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("CardID", v.getCardID());
		values.put("datepost", v.getdatepost());
		values.put("flag", v.getflag());
		values.put("note", v.getnote());
		values.put("AcountID", v.getAcoutID());

		// updating row
		return db.update("Card_", values, "CardID = ?",
				new String[] { String.valueOf(v.getCardID()) });
	}

	// Deleting single Card_
	public void deleteCard_(ECard_ v) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("Card_", "CardID = ?",
				new String[] { String.valueOf(v.getCardID()) });
		db.close();
	}

	// -------------Adding new IdentityCard_deck-------------------
	public void addIdentityCard_deck(EIdentityCard_deck v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		//values.put("ID", v.getID());
		values.put("deckName", v.getdeckName());
		values.put("Quantity", v.getQuantity());
		values.put("datepost", v.getdatepost());
		values.put("flag", v.getflag());
		values.put("note", v.getnote());
		// Inserting Row
		db.insert(TABLE_IDENTITYCARD_DECK, null, values);
		db.close(); // Closing database connection
	}

	// -------------Getting single IdentityCard_deck-------------------
	EIdentityCard_deck getIdentityCard_deck(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_IDENTITYCARD_DECK, new String[] { "ID",
				"deckName", "Quantity", "datepost", "flag", "note" }, "ID"
				+ "=?", new String[] { String.valueOf(id) }, null, null, null,
				null);
		if (cursor != null)
			cursor.moveToFirst();
		EIdentityCard_deck v = new EIdentityCard_deck(Integer.parseInt(cursor
				.getString(0)), cursor.getString(1), Integer.parseInt(cursor
				.getString(2)), cursor.getString(3), cursor.getString(4),
				cursor.getString(5));
		db.close();
		return v;
	}

	// -------------Getting All IdentityCard_deck-------------------
	public List<EIdentityCard_deck> getAllIdentityCard_deck() {
		List<EIdentityCard_deck> IdentityCard_deckList = new ArrayList<EIdentityCard_deck>();

		// Select All Query
		String selectQuery = "SELECT  * FROM IdentityCard_deck";
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				EIdentityCard_deck v = new EIdentityCard_deck();
				v.setID(Integer.parseInt(cursor.getString(0)));
				v.setdeckName(cursor.getString(1));
				v.setQuantity(Integer.parseInt(cursor.getString(2)));
				v.setdatepost(cursor.getString(3));
				v.setflag(cursor.getString(4));
				v.setnote(cursor.getString(5));

				// Adding contact to list
				IdentityCard_deckList.add(v);
			} while (cursor.moveToNext());
		}
		// return IdentityCard_deck list
		db.close();
		return IdentityCard_deckList;
	}

	// Updating single IdentityCard_deck

	public int updateIdentityCard_deck(EIdentityCard_deck v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("ID", v.getID());
		values.put("deckName", v.getdeckName());
		values.put("Quantity", v.getQuantity());
		values.put("datepost", v.getdatepost());
		values.put("flag", v.getflag());
		values.put("note", v.getnote());

		// updating row
		return db.update("IdentityCard_deck", values, "ID = ?",
				new String[] { String.valueOf(v.getID()) });
	}

	// Deleting single IdentityCard_deck
	public void deleteIdentityCard_deck(EIdentityCard_deck v) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("IdentityCard_deck", "ID = ?",
				new String[] { String.valueOf(v.getID()) });
		db.close();
	}

	// -------------Adding new Come_in-------------------
	public void addCome_in(ECome_in v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		//values.put("ID", v.getID());
		values.put("Time_In", v.getTime_In());
		values.put("AcountID", v.getAcoutID());
		values.put("GateID", v.getGateID());
		values.put("UserID", v.getUserID());
		// Inserting Row
		db.insert(TABLE_COME_IN, null, values);
		db.close(); // Closing database connection
	}

	// -------------Getting single Come_in-------------------
	ECome_in getCome_in(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_COME_IN, new String[] { "ID", "Time_In",
				"AcountID", "GateID", "UserID" }, "ID" + "=?",
				new String[] { String.valueOf(id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		ECome_in v = new ECome_in(Integer.parseInt(cursor.getString(0)),
				cursor.getString(1), Integer.parseInt(cursor.getString(2)),
				Integer.parseInt(cursor.getString(3)), Integer.parseInt(cursor
						.getString(4)));
		db.close();
		return v;
	}

	// -------------Getting All Come_in-------------------
	public List<ECome_in> getAllCome_in() {
		List<ECome_in> Come_inList = new ArrayList<ECome_in>();

		// Select All Query
		String selectQuery = "SELECT  * FROM Come_in";
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				ECome_in v = new ECome_in();
				v.setID(Integer.parseInt(cursor.getString(0)));
				v.setTime_In(cursor.getString(1));
				v.setAcoutID(Integer.parseInt(cursor.getString(2)));
				v.setGateID(Integer.parseInt(cursor.getString(3)));
				v.setUserID(Integer.parseInt(cursor.getString(4)));

				// Adding contact to list
				Come_inList.add(v);
			} while (cursor.moveToNext());
		}
		// return Come_in list
		db.close();
		return Come_inList;
	}

	// Updating single Come_in

	public int updateCome_in(ECome_in v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("ID", v.getID());
		values.put("Time_In", v.getTime_In());
		values.put("AcountID", v.getAcoutID());
		values.put("GateID", v.getGateID());
		values.put("UserID", v.getUserID());

		// updating row
		return db.update("Come_in", values, "ID = ?",
				new String[] { String.valueOf(v.getID()) });
	}

	// Deleting single Come_in
	public void deleteCome_in(ECome_in v) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("Come_in", "ID = ?",
				new String[] { String.valueOf(v.getID()) });
		db.close();
	}

	// -------------Adding new Out_side-------------------
	public void addOut_side(EOut_side v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		//values.put("ID", v.getID());
		values.put("GateID", v.getGateID());
		values.put("Time_In", v.getTime_In());
		values.put("Time_out", v.getTime_out());
		values.put("AcountID", v.getAcoutID());
		// Inserting Row
		db.insert(TABLE_OUT_SIDE, null, values);
		db.close(); // Closing database connection
	}

	// -------------Getting single Out_side-------------------
	EOut_side getOut_side(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_OUT_SIDE, new String[] { "ID", "GateID",
				"Time_In", "Time_out", "AcountID" }, "ID" + "=?",
				new String[] { String.valueOf(id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		EOut_side v = new EOut_side(Integer.parseInt(cursor.getString(0)),
				Integer.parseInt(cursor.getString(1)), cursor.getString(2),
				cursor.getString(3), Integer.parseInt(cursor.getString(4)));
		db.close();
		return v;
	}

	// -------------Getting All Out_side-------------------
	public List<EOut_side> getAllOut_side() {
		List<EOut_side> Out_sideList = new ArrayList<EOut_side>();

		// Select All Query
		String selectQuery = "SELECT  * FROM Out_side";
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				EOut_side v = new EOut_side();
				v.setID(Integer.parseInt(cursor.getString(0)));
				v.setGateID(Integer.parseInt(cursor.getString(1)));
				v.setTime_In(cursor.getString(2));
				v.setTime_out(cursor.getString(3));
				v.setAcoutID(Integer.parseInt(cursor.getString(4)));

				// Adding contact to list
				Out_sideList.add(v);
			} while (cursor.moveToNext());
		}
		// return Out_side list
		db.close();
		return Out_sideList;
	}

	// Updating single Out_side

	public int updateOut_side(EOut_side v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("ID", v.getID());
		values.put("GateID", v.getGateID());
		values.put("Time_In", v.getTime_In());
		values.put("Time_out", v.getTime_out());
		values.put("AcountID", v.getAcoutID());

		// updating row
		return db.update("Out_side", values, "ID = ?",
				new String[] { String.valueOf(v.getID()) });
	}

	// Deleting single Out_side
	public void deleteOut_side(EOut_side v) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("Out_side", "ID = ?",
				new String[] { String.valueOf(v.getID()) });
		db.close();
	}

	// -------------Adding new Identity_log-------------------
	public void addIdentity_log(EIdentity_log v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		//values.put("ID", v.getID());
		values.put("deckID", v.getdeckID());
		values.put("index", v.getindex());
		values.put("AcountID", v.getAcoutID());
		values.put("UserID", v.getUserID());
		// Inserting Row
		db.insert(TABLE_IDENTITY_LOG, null, values);
		db.close(); // Closing database connection
	}

	// -------------Getting single Identity_log-------------------
	EIdentity_log getIdentity_log(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_IDENTITY_LOG, new String[] { "ID",
				"deckID", "index", "AcountID", "UserID" }, "ID" + "=?",
				new String[] { String.valueOf(id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		EIdentity_log v = new EIdentity_log(Integer.parseInt(cursor
				.getString(0)), Integer.parseInt(cursor.getString(1)),
				Integer.parseInt(cursor.getString(2)), Integer.parseInt(cursor
						.getString(3)), Integer.parseInt(cursor.getString(4)));
		db.close();
		return v;
	}

	// -------------Getting All Identity_log-------------------
	public List<EIdentity_log> getAllIdentity_log() {
		List<EIdentity_log> Identity_logList = new ArrayList<EIdentity_log>();

		// Select All Query
		String selectQuery = "SELECT  * FROM Identity_log";
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				EIdentity_log v = new EIdentity_log();
				v.setID(Integer.parseInt(cursor.getString(0)));
				v.setdeckID(Integer.parseInt(cursor.getString(1)));
				v.setindex(Integer.parseInt(cursor.getString(2)));
				v.setAcoutID(Integer.parseInt(cursor.getString(3)));
				v.setUserID(Integer.parseInt(cursor.getString(4)));

				// Adding contact to list
				Identity_logList.add(v);
			} while (cursor.moveToNext());
		}
		// return Identity_log list
		db.close();
		return Identity_logList;
	}

	// Updating single Identity_log

	public int updateIdentity_log(EIdentity_log v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("ID", v.getID());
		values.put("deckID", v.getdeckID());
		values.put("index", v.getindex());
		values.put("AcountID", v.getAcoutID());
		values.put("UserID", v.getUserID());

		// updating row
		return db.update("Identity_log", values, "ID = ?",
				new String[] { String.valueOf(v.getID()) });
	}

	// Deleting single Identity_log
	public void deleteIdentity_log(EIdentity_log v) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("Identity_log", "ID = ?",
				new String[] { String.valueOf(v.getID()) });
		db.close();
	}

	public void deleteAccount_(int v) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("Account_", "AcountID = ?", new String[] { String.valueOf(v) });
		db.close();
	}

	public long countUser() {
		SQLiteDatabase db = this.getWritableDatabase();
        return DatabaseUtils.queryNumEntries(db,"Users_");

    }
	
	public int changepass(String uname,String pass, int id)
	{
		int b=0;
		SQLiteDatabase db = this.getWritableDatabase();
		
		//Cursor dataCount = db.rawQuery("select ID from Users_ where UserName="+"'" + uname +"'"+ " and Password="+"'"+pass+"'", null);
		 
		//if (dataCount.moveToFirst()){
			  // do{
			     
				  // int data = Integer.parseInt(dataCount.getString(0));
			      // do what ever you want here
			    //  Log.d("Gia tri: ", data + "");			     
			       // int id = dataCount.getInt(0);
			       // Log.d("ID: ", id+"");
		
			        ContentValues values = new ContentValues();
					values.put("Password", pass);
					 b= db.update("Users_", values, "ID = ?",
							new String[] { String.valueOf(id) });
					
			   //}while(dataCount.moveToNext());
			//}
		//dataCount.close();
		
		
        return b;
	}
	
	// -------------Getting All EOut_side_total-------------------
		public List<EOut_side_total> getAllOutsite_Insite(String date) {
			List<EOut_side_total> list = new ArrayList<EOut_side_total>();

			// Select All Query
			String selectQuery = "select o.ID,o.Time_in,o.Time_out,o.AcountID,o.GateID,g.Name,a.AccountName,c.CardKey,c.CardName,u.Fullname,u.UserName,a.Photo "
					+ " from Out_side o "
					+ " inner join Account_ a on o.AcountID=a.AcountID "
					+ " inner join Users_ u on a.UserID=u.ID "
					+ " inner join Gate_ g on o.GateID = g.ID "
					+ " inner join Card_ c on a.AcountID = c.AcountID"
					+ " where o.Time_In between '"+date+" 00:00:00' and '"+date+" 23:59:59'";
			Log.d("chuoi ", selectQuery);
					
			SQLiteDatabase db = this.getWritableDatabase();
			Cursor cursor = db.rawQuery(selectQuery, null);
			// looping through all rows and adding to list
			if (cursor.moveToFirst()) {
				do {
					EOut_side_total v = new EOut_side_total();
					v.setID(Integer.parseInt(cursor.getString(0)));
					v.setTime_In(cursor.getString(1));
					v.setTime_out(cursor.getString(2));
					v.setAcoutID(Integer.parseInt(cursor.getString(3)));
					v.setGateID(Integer.parseInt(cursor.getString(4)));
					v.setvGateName(cursor.getString(5));
					v.setvAcountName(cursor.getString(6));
					v.setvCardKey(cursor.getString(7));
					v.setvCardName(cursor.getString(8));
					v.setvFullname(cursor.getString(9));
					v.setvImg(cursor.getString(11));
					// Adding contact to list
					list.add(v);
				} while (cursor.moveToNext());
			}
			// return Account_ list
			db.close();
			return list;
		}
	
}
